package com.guochenglang.www.controller;

import com.guochenglang.www.pojo.Reply;
import com.guochenglang.www.pojo.User;
import com.guochenglang.www.service.ReplyService;
import com.guochenglang.www.service.UserService;
import com.guochenglang.www.util.GetObject;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.util.RequestReCodeUtil;
import com.guochenglang.www.view.vo.ArticleReplyView;
import com.guochenglang.www.view.vo.ReplyView;
import com.guochenglang.www.web.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

/**
 * 回复Controller层
 *
 * @author GuoChenglang
 */

public class ReplyController {
    private static final ReplyService REPLY_SERVICE = MyDependency.returnObject(ReplyService.class);
    private static final UserService USER_SERVICE = MyDependency.returnObject(UserService.class);

    /**
     * 获得评论的所有回复
     */
    public static Result getReply(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得评论的id
        int commentId = Integer.parseInt(request.getParameter("commentId"));
        //根据评论的id获取回复列表
        Reply allReply = new Reply();
        allReply.setCommentId(commentId);
        ArrayList<Reply> reply = REPLY_SERVICE.getReply(allReply);
        ArrayList<ArticleReplyView> articleReplyViews = new ArrayList<>();
        //根据回复里的用户获取用户名，返回前端显示
        for (Reply aReply : reply) {
            ArticleReplyView articleReplyView = new ArticleReplyView();
            User user = USER_SERVICE.getUser(aReply.getUserId());
            articleReplyView.setUsername(user.getUsername());
            articleReplyView.setReply(aReply);
            articleReplyView.setUserId(user.getId());
            Integer replyUserId = aReply.getReplyUser();
            User replyUser = USER_SERVICE.getUser(replyUserId);
            articleReplyView.setReplyUsername(replyUser.getUsername());
            articleReplyViews.add(articleReplyView);
        }
        return Result.result(articleReplyViews);
    }

    /**
     * 进行回复
     */
    public static Result uploadReply(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //编码设置
        RequestReCodeUtil.reCode(request, response);
        //获得评论id
        Integer commentId = GetObject.getValue(request, Integer.class, "commentId");
        //获得用户id
        Integer userId = GetObject.getValue(request, Integer.class, "userId");
        //获得回复
        String reply = GetObject.getValue(request, String.class, "reply");
        //被回复的用户id
        Integer replyUser = GetObject.getValue(request, Integer.class, "replyUser");
        //将信息封装进Reply中
        Reply replyObj = new Reply(null, replyUser, reply, null, userId, commentId);
        replyObj.setReplyTime(new Timestamp(System.currentTimeMillis()));
        int num = REPLY_SERVICE.uploadReply(replyObj);
        //含有违禁词
        if (num < 0) {
            return Result.dangerWord();
        }
        return Result.result(num);
    }

    /**
     * 删除回复
     */
    public static Result deleteReply(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获得回复id
        Integer replyId = GetObject.getObject(request, Integer.class);
        int num = REPLY_SERVICE.deleteReply(replyId);
        return Result.result(num);
    }
}
